package com.lv.service.dialog

import android.content.Context
import android.view.View
import android.widget.TextView
import com.github.gzuliyujiang.wheelpicker.widget.LinkageWheelLayout
import com.lv.common.im.OnPositionMoreCancle
import com.lv.service.R
import com.lv.service.bean.TelThirdCategory
import com.lv.service.room.category.CategoryBean
import com.lv.service.weidget.picker.ThirdLikeProvider
import com.lxj.xpopup.impl.PartShadowPopupView

/**
 * 作者： ygx
 * 创建日期：2022/10/10 10:32 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 */
class CardCategoryTop3PopupView(
    private val mContext: Context,
    private val mAllBeans: List<CategoryBean>?,
    private var mWheelPosition1: Int = 0,
    private var mWheelPosition2: Int = 0,
    private var mWheelPosition3: Int = 0,
    private var mWheelPos: Int? = -1,//显示第几个 -1 全部显示
    private val positionListener: OnPositionMoreCancle<TelThirdCategory>? = null,
    private var mCancelText: String = "重置",

    ) :
    PartShadowPopupView(mContext) {
    private var tvConfirm: TextView? = null
    private var tvCancel: TextView? = null
    private var wheelLinkage: LinkageWheelLayout? = null
    private var mDismissTag: Int = 0//0 确认 取消关闭  1 背景关闭
    private var viewBottom: View? = null

    override fun getImplLayoutId(): Int {
        return R.layout.card_third_top_popup
    }

    override fun onCreate() {
        super.onCreate()
        viewBottom = findViewById<View>(R.id.view_bottom)
        tvConfirm = findViewById<TextView>(R.id.tv_confirm)
        wheelLinkage = findViewById<LinkageWheelLayout>(R.id.wheel_linkage)
        tvCancel = findViewById<TextView>(R.id.tv_cancel)
        tvCancel?.text = mCancelText
        initView()
        initEvnet()
    }

    private fun initView() {

        val thirdLikeProvider = ThirdLikeProvider(mAllBeans)
        wheelLinkage?.setData(thirdLikeProvider)

        wheelLinkage?.setDefaultValue(mWheelPosition1, mWheelPosition2, mWheelPosition3)
        when (mWheelPos) {
            0 -> {
                wheelLinkage?.firstWheelView?.visibility = View.VISIBLE
                wheelLinkage?.secondWheelView?.visibility = View.GONE
                wheelLinkage?.thirdWheelView?.visibility = View.GONE
            }
            1 -> {
                wheelLinkage?.firstWheelView?.visibility = View.VISIBLE
                wheelLinkage?.secondWheelView?.visibility = View.VISIBLE
                wheelLinkage?.thirdWheelView?.visibility = View.GONE
            }
            else -> {
                wheelLinkage?.firstWheelView?.visibility = View.VISIBLE
                wheelLinkage?.secondWheelView?.visibility = View.VISIBLE
                wheelLinkage?.thirdWheelView?.visibility = View.VISIBLE
            }

        }


    }

    private fun initEvnet() {
        wheelLinkage?.setOnLinkageSelectedListener { first, second, third ->
            mWheelPosition1 = wheelLinkage?.firstWheelView?.currentPosition ?: 0
            mWheelPosition2 = wheelLinkage?.secondWheelView?.currentPosition ?: 0
            mWheelPosition3 = wheelLinkage?.thirdWheelView?.currentPosition ?: 0
//            mBinding.tvSearchCategory.text =
//                "${mAllBeans!![mWheelPosition1].name}-${mAllBeans[mWheelPosition1].telSecondCategories!![mWheelPosition2].name}"
        }
        tvConfirm?.setOnClickListener {
//            val item = "${mAllBeans!![mWheelPosition1].name}-${mAllBeans[mWheelPosition1].telSecondCategories!![mWheelPosition2].name}-${mAllBeans[mWheelPosition1].telSecondCategories!![mWheelPosition2].telThirdCategories!![mWheelPosition3].name}"
            var item: TelThirdCategory? = null
            if (mWheelPos == -1) {
                item =
                    mAllBeans!![mWheelPosition1].telSecondCategories!![mWheelPosition2].telThirdCategories!![mWheelPosition3]
            }
            positionListener?.onConfirmClick(
                mWheelPosition1,
                mWheelPosition2,
                mWheelPosition3,
                data = item
            )
            mDismissTag = 0
            dismiss()
        }
        tvCancel?.setOnClickListener {
            mDismissTag = 1
            dismiss()
        }
        viewBottom?.setOnClickListener {
            mDismissTag = 0
            dismiss()
        }
    }


    override fun onDestroy() {
        super.onDestroy()
    }

    override fun beforeDismiss() {
        super.beforeDismiss()
        positionListener?.onConcelClick(mDismissTag)

    }


}